package com.gupaoedu.proxypattern.proxy.dynamicproxy.cglibproxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibMeipo implements MethodInterceptor {

    public Object newInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }


    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        before();
        Object re = methodProxy.invokeSuper(o, args);
        after();
        return re;
    }

    private void after() {
        System.out.println("我是媒婆，介绍完毕！");
    }

    private void before() {
        System.out.println("我是媒婆，开始介绍对象");
    }
}
